﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataClient.ClientDatasetTableAdapters;
using CommonData.CommonItems;
using CommonData;

namespace DataClient
{
    public partial class MainForm : DevExpress.XtraEditors.XtraForm
    {
        const string STR_NOT_SYNC = "Chưa đồng bộ";
        const string STR_IS_SYNC = "Đồng bộ thành công";
        List<string> lstTable = new List<string>() { "Customer", "DspOrdDet", "INTran", "Inventory", "SaleRouteDetail", "Salesperson", "SalesRoute", "SaleTeam", "SlsOrdDsp" };
        MainUsecase _UCMain;
        public MainForm()
        {
            InitializeComponent();
            _UCMain = new MainUsecase();
        }

        private bool isSynchronied(string strTable)
        {
            return dsClient.SyncTracking.Select(string.Format("Items = '{0}'", strTable)).Length > 0; 
        }

        private void InitDataOnform()
        {
            daTracking.Fill(dsClient.SyncTracking, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date);
            foreach (string strTable in lstTable)
            {
                if (!isSynchronied(strTable))
                {
                    ClientDataset.SyncTrackingRow drNewSync = dsClient.SyncTracking.NewSyncTrackingRow();
                    drNewSync.Selected = false;
                    drNewSync.SyncDate = DateTime.Now;
                    drNewSync.Items = strTable;
                    drNewSync.Status = STR_NOT_SYNC;
                    dsClient.SyncTracking.AddSyncTrackingRow(drNewSync);
                }
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            dtpFromDate.DateTime = DateTime.Now;
            dtpToDate.DateTime = DateTime.Now;
            ValueListClass clValueList = new ValueListClass();
            DataTable tblDBConfig = clValueList.GetDatabaseConfigList();
            iluSConfig.Properties.DataSource = tblDBConfig;
            iluDConfig.Properties.DataSource = tblDBConfig;

            iluSConfig.EditValue = DataClient.Properties.Settings.Default.SConfigValue;
            iluDConfig.EditValue = DataClient.Properties.Settings.Default.DConfigValue;

            InitDataOnform();
        }

        private void viewSync_ShowingEditor(object sender, CancelEventArgs e)
        {
            if (viewSync.GetDataRow(viewSync.FocusedRowHandle) is ClientDataset.SyncTrackingRow &&
                ((ClientDataset.SyncTrackingRow)viewSync.GetDataRow(viewSync.FocusedRowHandle)).Status.Equals(STR_IS_SYNC))
            {
                e.Cancel = true;
            }
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            PasswordConfigForm frmPassConfig = new PasswordConfigForm();
            if (frmPassConfig.ShowDialog() == DialogResult.OK)
            {
                iluSConfig.EditValue = DataClient.Properties.Settings.Default.SConfigValue;
                iluSConfig.EditValue = DataClient.Properties.Settings.Default.DConfigValue;
            }
        }

        private void btnSync_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (chkClearBeforeSync.Checked)
            {
                if (MessageClass.ShowQuestionMessage("Việc xóa toàn bộ dữ liệu dễ gây mất an toàn cho hệ thống!\nBạn có chắc là muốn xóa toàn bộ dữ liệu trước khi đồng bộ không?") == DialogResult.Yes)
                {
                    pnProgress.Visible = true;
                    pnProgress.Refresh();
                    _UCMain.ProcessSynchronizeData(dsClient,daTracking, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date, chkClearBeforeSync.Checked, rdgSynType.SelectedIndex == 1);
                }
            }
            else
            {
                pnProgress.Visible = true;
                pnProgress.Refresh();
                _UCMain.ProcessSynchronizeData(dsClient,daTracking, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date, chkClearBeforeSync.Checked, rdgSynType.SelectedIndex == 1);
            }
            pnProgress.Visible = false;
            this.Cursor = Cursors.Default;
        }

        private void rdgSynType_SelectedIndexChanged(object sender, EventArgs e)
        {
            dtpFromDate.Enabled = rdgSynType.SelectedIndex == 0;
            dtpToDate.Enabled = rdgSynType.SelectedIndex == 0;
        }

        private void DateTime_Changed(object sender, EventArgs e)
        {
            InitDataOnform();
        }
    }
}
